package com.vtech.mybatis.baseservice.plugins;

import com.vtech.mybatis.baseservice.util.PluginUtil;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.internal.util.StringUtility;

/**
 * TODO
 * 扩展的额外方法
 *
 * @author houzhiwei
 * @Date 2016/11/7 20:07.
 */
public class AdditionalMethodsSqlMap
{
    private static final String DELETE_BY_COLUMN = "deleteByColumn";
    private static final String SELECT_BY_COLUMN = "selectByColumn";
    private static final String PARAM_COLUMN_NAME = "columnName";
    private static final String PARAM_COLUMN_VALUE = "columnValue";


    //生成deleteByColumn方法的sqlmap
    public void generateDeleteByColumnSqlMap(Document document, IntrospectedTable
            introspectedTable, Context context)
    {
        XmlElement deleteEl = new XmlElement("delete"); //$NON-NLS-1$
        String methodName = DELETE_BY_COLUMN;
        deleteEl.addAttribute(new Attribute("id", methodName));
        deleteEl.addAttribute(new Attribute("parameterType", "map"));

        context.getCommentGenerator().addComment(deleteEl);

        StringBuilder sb = new StringBuilder();
        sb.append("delete from "); //$NON-NLS-1$
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        sb.append(" where ");
        deleteEl.addElement(new TextElement(sb.toString()));
        XmlElement chooseElement = new XmlElement("choose");
        XmlElement whenElement = new XmlElement("when");
        whenElement.addAttribute(new Attribute("test", "columnValue == null"));
        whenElement.addElement(new TextElement("${columnName} is null"));
        chooseElement.addElement(whenElement);
        whenElement = new XmlElement("when");
        whenElement.addAttribute(new Attribute("test", "columnValue != null"));
        whenElement.addElement(new TextElement("${columnName} = #{columnValue}"));
        chooseElement.addElement(whenElement);
        deleteEl.addElement(chooseElement);

        XmlElement parentElement = document.getRootElement();
        parentElement.addElement(deleteEl);
    }

    public void generateSelectByColumnSqlMap(Document document, IntrospectedTable
            introspectedTable, boolean withBlobs, Context context)
    {
        XmlElement selectEl = new XmlElement("select");
        String methodName = SELECT_BY_COLUMN;
        String resultMap = introspectedTable.getBaseResultMapId();
        if (withBlobs) {
            methodName = methodName + "WithBLOBs";
            resultMap = introspectedTable.getResultMapWithBLOBsId();
        }
        selectEl.addAttribute(new Attribute("id", methodName));
        selectEl.addAttribute(new Attribute("resultMap", resultMap));
        selectEl.addAttribute(new Attribute("parameterType", "map"));

        context.getCommentGenerator().addComment(selectEl);

        selectEl.addElement(new TextElement("select distinct"));

        StringBuilder sb = new StringBuilder();

        if (StringUtility.stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
            sb.append('\'');
            sb.append(introspectedTable.getSelectByExampleQueryId());
            sb.append("' as QUERYID,");
            selectEl.addElement(new TextElement(sb.toString()));
        }
        sb.setLength(0);
        selectEl.addElement(PluginUtil.getBaseColumnListElement(introspectedTable));

        if (withBlobs) {
            selectEl.addElement(new TextElement(","));
            selectEl.addElement(getBlobColumnListElement(introspectedTable));
        }

        sb.append("from ");
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
        sb.append(" where ");
        selectEl.addElement(new TextElement(sb.toString()));
        XmlElement chooseElement = new XmlElement("choose");
        XmlElement whenElement = new XmlElement("when");
        whenElement.addAttribute(new Attribute("test", "columnValue == null"));
        whenElement.addElement(new TextElement("${columnName} is null"));
        chooseElement.addElement(whenElement);
        whenElement = new XmlElement("when");
        whenElement.addAttribute(new Attribute("test", "columnValue != null"));
        whenElement.addElement(new TextElement("${columnName} = #{columnValue}"));
        chooseElement.addElement(whenElement);
        selectEl.addElement(chooseElement);

        XmlElement parentElement = document.getRootElement();
        parentElement.addElement(selectEl);
    }

    protected XmlElement getBlobColumnListElement(IntrospectedTable introspectedTable)
    {
        XmlElement answer = new XmlElement("include");
        answer.addAttribute(new Attribute("refid", introspectedTable.getBlobColumnListId()));
        return answer;
    }
}
